// 角色管理接口
import request from '@/utils/request'
import type { ResRoleList, ResMenuList } from './type'
import { UserRole } from '../user/type'
enum API {
  get_rolelist = '/admin/acl/role/',
  add_role = '/admin/acl/role/save',
  update_role = '/admin/acl/role/update',
  delete_role = '/admin/acl/role/remove/',
  user_permission = '/admin/acl/permission/toAssign/',
  set_permission = '/admin/acl/permission/doAssignAcl',
}

// 获取角色列表
export const reqGetRoleList = (
  page: number,
  limit: number,
  roleName?: string,
) => {
  if (roleName)
    return request.get<any, ResRoleList>(
      API.get_rolelist + `${page}/${limit}?roleName=${roleName}`,
    )
  return request.get<any, ResRoleList>(API.get_rolelist + `${page}/${limit}`)
}

// 添加/修改角色
export const reqAddOrUpdateRole = (data: UserRole) => {
  // 存在ID，为修改角色
  if (data.id) return request.put<UserRole, any>(API.update_role, data)
  // 添加角色
  return request.post<UserRole, any>(API.add_role, data)
}

// 删除角色
export const reqDeleteRole = (id: number) =>
  request.delete<any, any>(API.delete_role + id)

// 获取全部菜单和用户菜单权限
export const reqUserPermission = (id: number) =>
  request.get<any, ResMenuList>(API.user_permission + id)

// 设置角色权限
export const reqSetPermission = (data: { roleId: number; permissionIdList: number[] }) =>
  request.post<any, any>(API.set_permission, data)
